using System.Data;
using System.Data.SQLite;
using Model;

namespace DAL {
    public class CategoryDAL {
        private CategoryDAL() {
        }

        private static CategoryDAL instance = new CategoryDAL();

        public static CategoryDAL GetInstance() {
            return instance;
        }


        public DataTable SelectAllCategory() {
            return SqlHelper.GetDataTable(new SQLiteCommand("select * from CategoryInfo where DelFlag = 0"));
        }

        public int UpdateCategory(Category category) {
            if (category.CatId == 0) {
                return SqlHelper.Update(
                    new SQLiteCommand(
                        "insert into CategoryInfo(CatName, Remark, SubTime) values (@CatName, @Remark, @SubTime)") {
                        Parameters = {
                            new SQLiteParameter("@CatName", category.CatName),
                            new SQLiteParameter("@Remark", category.Remark),
                            new SQLiteParameter("@SubTime", category.SubTime)
                        }
                    });
            }

            return SqlHelper.Update(
                new SQLiteCommand(
                    "update CategoryInfo set CatName = @CatName, Remark = @Remark, SubTime = @SubTime where CatId = @CatId") {
                    Parameters = {
                        new SQLiteParameter("@CatName", category.CatName),
                        new SQLiteParameter("@Remark", category.Remark),
                        new SQLiteParameter("@SubTime", category.SubTime),
                        new SQLiteParameter("@CatId", category.CatId)
                    }
                });
        }

        public int DeleteCategory(Category category) {
            return SqlHelper.Update(new SQLiteCommand("update CategoryInfo set DelFlag = 1 where CatId = @CatId") {
                Parameters = {
                    new SQLiteParameter("@CatId", category.CatId)
                }
            });
        }
    }
}